package com.atguigu.edumember.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.edumember.entity.UcenterMember;
import com.atguigu.edumember.entity.vo.LoginInfoVo;
import com.atguigu.edumember.entity.vo.LoginVo;
import com.atguigu.edumember.entity.vo.RegisterVo;
import com.atguigu.edumember.mapper.UcenterMemberMapper;
import com.atguigu.edumember.service.UcenterMemberService;
import com.atguigu.service.handler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2022-09-07
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"手机号或密码不能为空！");
        }
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember member = baseMapper.selectOne(wrapper);
        if(member==null){
            throw new GuliException(20001,"该手机号未注册！");
        }
        if(!member.getPassword().equals(MD5.encrypt(password))){
            throw new GuliException(20001,"密码错误！");
        }
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return token;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        if(mobile==null||nickname==null||password==null||code==null){
            throw new GuliException(20001,"注册失败，不能为空！");
        }
        String s = redisTemplate.opsForValue().get(mobile);
        if(!s.equals(code)){
            throw new GuliException(20001,"验证码错误！请重新输入~");
        }
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer integer = baseMapper.selectCount(wrapper);
        if(integer>0){
            throw new GuliException(20001,"该手机号已注册账号！");
        }
        UcenterMember member=new UcenterMember();
        member.setNickname(nickname);
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password));
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        member.setIsDisabled(0);
        this.save(member);
    }

    @Override
    public LoginInfoVo getUserInfoById(String memberId) {
        UcenterMember member = baseMapper.selectById(memberId);
        LoginInfoVo infoVo=new LoginInfoVo();
        BeanUtils.copyProperties(member,infoVo);
        return infoVo;
    }

    @Override
    public UcenterMember getByOpenid(String openid) {
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember one = this.getOne(wrapper);
        return one;
    }

    @Override
    public int countRegisterByDay(String day) {
        int count=baseMapper.countRegisterByDay(day);
        return count;
    }
}
